package com.kxf.androidtestdemo.activity;

import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.kxf.androidtestdemo.R;

public class JsbridgeActivity extends BaseActivity {

    private BridgeWebView bwv = null;
    private TextView tv_info;
    private StringBuffer sb = new StringBuffer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jsbridge);

        bwv = findViewById(R.id.bwv);
        tv_info = findViewById(R.id.tv_info);



        bwv.setWebContentsDebuggingEnabled(true);
        bwv.getSettings().setJavaScriptEnabled(true);// 支持运行javascript
        bwv.getSettings().setDomStorageEnabled(true);

//        bwv.setWebChromeClient(new WebChromeClient() {
//            @Override
//            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
//                Log.d("WebViewActivity", consoleMessage.message() + " -- (" + consoleMessage.messageLevel() + ")From line "
//                        + consoleMessage.lineNumber() + " of "
//                        + consoleMessage.sourceId());
//                return true;
//            }
//
//            @Override
//            public void onConsoleMessage(String message, int lineNumber, String sourceID) {
//                super.onConsoleMessage(message, lineNumber, sourceID);
//            }
//        });// 支持运行特殊的javascript（例如：alert()）

//        bwv.setWebViewClient(new WebViewClient() {
//            @Override
//            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//                Log.d("WebViewActivity", "onReceivedSslError SslError=" + error);
//                handler.proceed();
//            }
//        });// 当点击超链地址后不会新打开浏览器来访问，而是始终在本app中浏览页面

//        bwv.setWebChromeClient(new WebChromeClient());

        bwv.setDefaultHandler(new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                Log.d(tag, "java接收到js请求数据 默认方法 data=" + data);
                showInfo("java接收到js请求数据 默认方法 data=" + data);
                function.onCallBack("js你好！我是Native默认方法，收到你的消息了！");
            }
        });

        bwv.registerHandler("mJsCallNativeTest", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                Log.d(tag, "java接收到js请求数据  mJsCallNativeTest data=" + data);
                showInfo("java接收到js请求数据  mJsCallNativeTest data=" + data);
                function.onCallBack("js你好！我是Native 指定接口mJsCallNativeTest，收到你的消息了！");
            }
        });

        bwv.loadUrl("file:///android_asset/demo.html");
    }

    public void sendTojs(View view) {
        bwv.send("js默认方法你好，我是Native", new CallBackFunction() {
            @Override
            public void onCallBack(String data) {
                Log.d(tag, "Native接收到js默认方法返回 data=" + data);
                showInfo("Native接收到js默认方法返回 data=" + data);
            }
        });
    }

    public void sendTojsM(View view) {
        bwv.callHandler("mNativeCallJavaTest", "js指定方法mNativeCallJavaTest你好，我是Native", new CallBackFunction() {
            @Override
            public void onCallBack(String data) {
                Log.d(tag, "Native接收到js指定方法返回数据  mNativeCallJavaTest data=" + data);
                showInfo("Native接收到js指定方法返回数据  mNativeCallJavaTest data=" + data);
            }
        });
    }

    private void showInfo(final String str){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                sb.append(str + "\n\r");
                tv_info.setText(sb.toString());
            }
        });
    }
}
